package com.ksd1.notes;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.ksd1.notes.db.NoteDB;
import com.ksd1.notes.markdown.MDWriter;
import com.ksd1.notes.util.TimeTool;

import java.util.Calendar;


public class NoteActivity extends BaseActivity {
    private EditText mNoteEditText;
    private MDWriter mMDWriter;
    private NoteDB.Note mNote;
    private LinearLayout bottomToolbar;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initVariables() {
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_edit_test1);

        //上方的工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);//显示返回箭头
            actionBar.setDisplayShowHomeEnabled(false);//不显示名字
        }

        mNoteEditText = findViewById(R.id.NoteEditText);//编辑框
        mNoteEditText.setFocusable(true);
        mNoteEditText.setFocusableInTouchMode(true);
        mNoteEditText.requestFocus();

        //提醒
        showRemainDialog();
    }

    /**
     * 提示用户操作
     */
    private void showRemainDialog(){
        if(TimeTool.getInstance().getField("dremain").equals("1")) return;

        /*Snackbar.make(getWindow().getDecorView(), "退出编辑时会自动保存哦！", Snackbar.LENGTH_SHORT).setAction("不再提醒", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeTool.getInstance().setField("dremain", "1");
            }
        }).show();*/

        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Tip：");
        dialog.setMessage("退出编辑时会自动保存哦！");
        dialog.setCancelable(true);
        dialog.setPositiveButton("不再提醒", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                TimeTool.getInstance().setField("dremain", "1");
            }
        });

        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.show();
    }

    @Override
    protected void loadData() {
        mMDWriter = new MDWriter(mNoteEditText);
        long key = getIntent().getLongExtra("NoteId", -1);//主键
        position = getIntent().getIntExtra("position", -1);//在列表中的位置

        if (key != -1) {//不是新打开的文件
            mNote = NoteDB.getInstance().nget(key);
            if (mNote != null) {
                mMDWriter.setContent(mNote.content);
            } else {
                mNote = new NoteDB.Note();
                mNote.key = -1;
            }

        } else {
            mNote = new NoteDB.Note();
            mNote.key = -1;
        }
    }

    //上方菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_note, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //显示
        if (id == R.id.action_display) {
            Intent intent = new Intent(this, DisplayActivity.class);
            intent.putExtra("Content", mMDWriter.getContent());
            startActivity(intent);
            return true;
        } else if (id == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    //标题
    public void onClickHeader(View v) {
        mMDWriter.setAsHeader();
    }

    //居中
    public void onClickCenter(View v) {
        mMDWriter.setAsCenter();
    }

    //列表
    public void onClickList(View v) {
        mMDWriter.setAsList();
    }

    //加粗
    public void onClickBold(View v) {
        mMDWriter.setAsBold();
    }

    //引用
    public void onClickQuote(View v) {
        mMDWriter.setAsQuote();
    }


    @Override
    protected void onPause() {
        onSaveNote();
        super.onPause();
    }

    /**
     * 保存数据
     */
    public void onSaveNote() {
        mNote.title = mMDWriter.getTitle();
        mNote.content = mMDWriter.getContent();
        if (mNote.key == -1) {
            mNote.type = MainActivity.currentType == 0? 1 : MainActivity.currentType;//默认是未分类
            if (!"".equals(mNote.content)) {
                mNote.date = Calendar.getInstance().getTimeInMillis();
                mNote.key = NoteDB.getInstance().ninsert(mNote);
                position = 0;
                notifyChange(true);
            }
        } else {//之前存在
//            Log.d("20182005085", "更新: "+mNote.key);
            NoteDB.getInstance().nupdate(mNote.key, mNote);//更新
            notifyChange(false);
        }

    }

    /**
     * 通知消息发生变化
     * @param flag 插入true 删除false
     */
    private void notifyChange(boolean flag){
        if(flag){//新消息
            //通知新数据插入
            MainActivity.noteList.add(0, mNote);//插入
            MainActivity.noteAdapteradapter.notifyItemInserted(0);
            MainActivity.note_recyclerView.scrollToPosition(0);
        }else{
            //通知更新
            MainActivity.noteList.get(position).title = mNote.title;
            MainActivity.noteList.get(position).key =mNote.key;
            MainActivity.noteList.get(position).title =mNote.title;
            MainActivity.noteList.get(position).type =mNote.type;

            MainActivity.noteAdapteradapter.notifyItemChanged(position);
        }
    }
}
